#include<iostream>
#include<algorithm>
using namespace std;
const int N = 1e3 + 10;
int f[N][N];
int w[N][N];
int n, m;
int dx[] = { -1,0,1,0 };
int dy[] = { 0,1,0,-1 };
int dp(int x, int y)
{
	if (f[x][y] != -1)return f[x][y];
	f[x][y] = 1;
	for (int i = 0; i < 4; i++) {
		int a = dx[i] + x;
		int b = dy[i] + y;
		if (a >= 1 && a <= n && b >= 1 && b <= m && w[a][b] < w[x][y]) {
			f[x][y] = max(f[x][y], dp(a, b) + 1);
		}
	}
	return f[x][y];
}
int main()
{
	cin >> n >> m;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			cin >> w[i][j];
		}
	}
	memset(f, -1, sizeof(f));
	int ans = 0;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			ans = max(ans, dp(i, j));
		}
	}	
	cout << ans << endl;
	return 0;
}